package tmproject.hlhj.fhp.cartrans.activitys

import android.content.Intent
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.transition.Explode
import android.transition.Fade
import android.view.View
import com.huantansheng.easyphotos.EasyPhotos
import com.tencent.bugly.Bugly
import com.tencent.bugly.beta.Beta
import com.zhihu.matisse.Matisse
import kotlinx.android.synthetic.main.aty_home.*
import org.greenrobot.eventbus.EventBus
import tmproject.hlhj.fhp.cartrans.BaseAty
import tmproject.hlhj.fhp.cartrans.HomePagerAdp
import tmproject.hlhj.fhp.cartrans.IView.HomeView
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.customVIews.WecomeDialog
import tmproject.hlhj.fhp.cartrans.event.PathEvent
import tmproject.hlhj.fhp.cartrans.fragments.*
import tmproject.hlhj.fhp.cartrans.javaBean.DriverInfoBean
import tmproject.hlhj.fhp.cartrans.javaBean.LearInfoBean
import tmproject.hlhj.fhp.cartrans.javaBean.UnReadNumBean
import tmproject.hlhj.fhp.cartrans.log
import tmproject.hlhj.fhp.cartrans.presenter.HomePresenter
import tmproject.hlhj.fhp.cartrans.utils.SpHelper
import java.util.ArrayList

/**
 * Created by Administrator on 2018/8/26/026.
 */
class HomeAty :BaseAty<HomeView,HomePresenter>(), HomeView {
    private val fgmLists=ArrayList<Fragment>()
    private lateinit var userCenterFgm:Fragment
    private lateinit var learnFgm:Fragment
    private lateinit var noticeFgm:Fragment
    private lateinit var reviewFgm:Fragment
    private lateinit var examFgm:Fragment
    private lateinit var pagerAdp:HomePagerAdp
    private var tempA =true
    private var tempB =true
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP_MR1)
    override fun initView() {
//        window.enterTransition = Explode().setDuration(2000)

//        WecomeDialog(this).show()

        rbLearn.isChecked=true

        learnFgm=LearnFragment()
        userCenterFgm=UserCenterFgm()
        noticeFgm=NoticeFgm()
        reviewFgm=ReViewFgm()
        examFgm=ExamFgm()
        fgmLists.add(learnFgm)
        fgmLists.add(reviewFgm)
        fgmLists.add(noticeFgm)
        fgmLists.add(examFgm)
        fgmLists.add(userCenterFgm)
        pagerAdp=HomePagerAdp(fgmLists,supportFragmentManager)
        viewPager.adapter=pagerAdp
    }
    override fun logicStart() {
        presenter?.getInfo(this,SpHelper.getToken(this))
        presenter?.getUnReadNum(this,SpHelper.getToken(this))
    }

    override fun setLearn(learInfoBean: LearInfoBean) {

    }

    override fun onResume() {
        presenter?.getUnReadNum(this,SpHelper.getToken(this))
        super.onResume()
    }
    override fun setListener() {
        viewPager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                if (!tempB)return
                tempA =false
                when(position){
                    0->{
                        rbBottom.check(R.id.rbLearn)
                    }
                    1->{
                        rbBottom.check(R.id.rbReview)

                    }
                    2->{
                        rbBottom.check(R.id.rbNotice)

                    }
                    3->{
                        rbBottom.check(R.id.rbExam)

                    }
                    4->{
                        rbBottom.check(R.id.rbSelf)

                    }
                }
                tempA=true
            }
        })

        rbBottom.setOnCheckedChangeListener { group, checkedId ->
            if (!tempA)return@setOnCheckedChangeListener
            tempB=false
            when (checkedId){
                R.id.rbLearn->{
                    viewPager.currentItem=0
                }
                R.id.rbReview->{
                    viewPager.currentItem=1
                }
                R.id.rbNotice->{
                    viewPager.currentItem=2
                }
                R.id.rbExam->{
                    viewPager.currentItem=3
                }
                R.id.rbSelf->{
                    viewPager.currentItem=4
                }
            }
            tempB=true
        }

    }

    /**
     * 设置未读消息数量
     */
    override fun setUnReadNum(unReadNumBean: UnReadNumBean) {
        if (unReadNumBean.data.count>0){
            unReadNum.visibility=View.VISIBLE
            unReadNum.text=unReadNumBean.data.count.toString()
        }else{
            unReadNum.visibility=View.GONE
        }
    }

    override fun bindView(): HomeView {
        return this
    }

    override fun createPresenter(): HomePresenter {
        return HomePresenter()
    }

    override fun getContentId(): Int {
        return R.layout.aty_home
    }

    override fun setDriverInfo(driverInfoBean: DriverInfoBean) {
        SpHelper.saveDriver(this,driverInfoBean)

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        log("fhp","")
        if (resultCode!=-1)return
        when(requestCode){
//            拍照
            111->{
                val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
                val pathEvent = PathEvent()
                pathEvent.path=pathList[0]
                log("fhp","path为 ${pathList[0]}")
                EventBus.getDefault().post(pathEvent)
            }
        }
    }
}
